<?php
/**
 * Controller de Integracao.
 *
 * @author    Chavão <freela@chavao.net>
 * @copyright Copyright (c) 2011, Chavão
 * @version   0.1
 */

class IntegracaoController extends AppController
{
    public $layout = 'vazio';
    public $helpers = array("Html", "Form", "Campos");

    public function integrateHotSite( $piCampId )
    {
        $objCamp = (object) $this->Campanha->first(array(
			'conditions' => array( 'camp_id' => $piCampId )
        ));
        if($objCamp->camp_ativo == 1)
        {
            $arrCCamp = $this->CampanhaCampo->all(array(
			'conditions' => array( 'camp_id' => $piCampId )
            ));
            $arrCampo = array();
            foreach( $arrCCamp as $iValor )
            {
                $arrCampo[] = $iValor['cmps_id'];
            }
            $arrCampos = $this->Campo->all(array(
                            'conditions' => array( 'cmps_id' => $arrCampo )
            ));

            $sCSS = ( trim( $objCamp->camp_url_css ) != '' ) ? $objCamp->camp_url_css : null;
            $this->set( 'ativo', true );
            $this->set( 'arrCampos', $arrCampos );
            $this->set( 'iCampId', $piCampId );
            $this->set( 'sCSS', $sCSS );
        }
        else
        {
            $this->set( 'ativo', false );
        } 
    }

    public function adicionar()
    {
        $this->autoRender = false;

        if( !empty( $this->data ) )
        {
            $modelLead = new Lead();
            $oCamp = $this->Campanha->first(array(
                        'conditions' => array( 'camp_id' => $this->data['camp_id'] )
            ));
            $oCliente = $this->Cliente->first(array(
                        'conditions' => array( 'clie_id' => $oCamp['clie_id'] )
            ));
            $oPlano = $this->Plano->first(array(
                        'conditions' => array( 'plan_id' => $oCliente['plan_id'] )
            ));
           
            $arrCamp = $this->Campanha->all(array(
                        'conditions' => array( 'clie_id' => $oCliente['clie_id'] )
            ));
            $intLeads = 0;
            foreach ($arrCamp as $oCampanha) 
            {
                 $intLeads = $intLeads + $this->Lead->count(array(
                        'conditions' => array( 'camp_id' => $oCampanha['camp_id'] )
                 ));
            }
           
            if($oCamp['camp_fila'])
            {
                $oLead = $this->Lead->first(array(
                        'conditions' =>  array( 'camp_id='.$this->data['camp_id']." order by lead_entrada desc" )
                ));
                if($oLead['usua_id'] > 0) 
                {
                    $arrUsuarios = $this->Usuario->all(array(
			'conditions' => array( 'clie_id='.$oCamp['clie_id']." and (usua_ativo = 1) order by usua_id asc" )
                    ));
                    foreach ($arrUsuarios as $key => $usuario) 
                    {
                        if($usuario['usua_id'] == $oLead['usua_id']) 
                        {
                            if(($key+1) >= count($arrUsuarios))
                                $this->data['usua_id'] = $arrUsuarios[0]['usua_id'];
                            else
                                 $this->data['usua_id'] = $arrUsuarios[$key+1]['usua_id'];
                            if( $intLeads >= $oPlano['plan_leads'] )
                                $this->data['lead_bloqueado'] = 1;
                           else
                               $this->data['lead_bloqueado'] = 0;
                            
                            $modelLead->adicionar( $this->data );
                            $this->redirect($_SERVER['HTTP_REFERER']);
                            return;
                        }
                    }
                }
                else
                {
                    $oUsuario = $this->Usuario->first(array(
			'conditions' => array( 'clie_id='.$oCamp['clie_id']." order by usua_id asc" )
                    ));
                    $this->data['usua_id'] = $oUsuario['usua_id'];
                }
            }
            if( $intLeads >= $oPlano['plan_leads'] )
                 $this->data['lead_bloqueado'] = 1;
            else
                $this->data['lead_bloqueado'] = 0;
            $modelLead->adicionar( $this->data );
            $this->redirect($_SERVER['HTTP_REFERER']);
        }
    }
    public function code( $piCampId, $pmWidth = "250", $pmHeight = "500" )
    {
        $this->set('iCampId', $piCampId);
        $this->set('mWidth', $pmWidth);
        $this->set('mHeight', $pmHeight);
    }
}
?>